package cn.edu.tju.fctl.util;

import cn.edu.tju.fctl.entities.User;
import org.codemonkey.simplejavamail.Email;
import org.codemonkey.simplejavamail.Mailer;

import javax.mail.internet.MimeMessage;
import java.util.List;

/**
 * Created by catold on 15/12/22.
 */
public class EmailUtil {
    public static final String USERNAME = "catoldcui@163.com";
    public static final int PORT = 25;
    public static final String PWD = "cuihui1";
    public static final String SMTPURL = "smtp.163.com";
    public static final String SENDNAME = "admin";


    public static final String PREMESSAGE = "您好, ";
    public static final String AFTERMESSAGE = " 谢谢";

    /**
     * 发送至多个email
     * @param toEmails
     * @param subject
     * @param message
     */
    public static void sendMultiMail(List<String> toEmails, String subject, String message) {
        Email email = new Email();

        email.setFromAddress(SENDNAME, USERNAME);
        email.setSubject(subject);
        for (String str : toEmails) {
            if(str == null || str.isEmpty()){
                continue;
            }
            email.addRecipient("", str, MimeMessage.RecipientType.TO);
        }

        email.setText(PREMESSAGE + message + AFTERMESSAGE);

// embed images and include downloadable attachments
//        email.addEmbeddedImage("wink1", imageByteArray, "image/png");
//        email.addEmbeddedImage("wink2", imageDatesource);
//        email.addAttachment("invitation", pdfByteArray, "application/pdf");
//        email.addAttachment("dresscode", odfDatasource);

        new Mailer(SMTPURL, 25, USERNAME, PWD).sendMail(email);
    }

    /**
     * 发送至多个email至users
     * @param users
     * @param subject
     * @param message
     */
    public static void sendMultiMailtoUser(List<User> users, String subject, String message) {
        Email email = new Email();

        email.setFromAddress(SENDNAME, USERNAME);
        email.setSubject(subject);
        for (User user : users) {
            if(user.getEmail() != null && !user.getEmail().isEmpty()){
                email.addRecipient("", user.getEmail(), MimeMessage.RecipientType.TO);
            }
        }

        email.setText(PREMESSAGE + message + AFTERMESSAGE);

// embed images and include downloadable attachments
//        email.addEmbeddedImage("wink1", imageByteArray, "image/png");
//        email.addEmbeddedImage("wink2", imageDatesource);
//        email.addAttachment("invitation", pdfByteArray, "application/pdf");
//        email.addAttachment("dresscode", odfDatasource);

        new Mailer(SMTPURL, 25, USERNAME, PWD).sendMail(email);
    }

    /**
    * 发送至一个emails
     * @param toEmail
     * @param subject
     * @param message
     */
    public static void sendOneMail(String toEmail, String subject, String message) {
        if(toEmail == null || toEmail.isEmpty()){
            return;
        }
        Email email = new Email();

        email.setFromAddress(SENDNAME, USERNAME);
        email.setSubject(subject);
        email.addRecipient("", toEmail, MimeMessage.RecipientType.TO);

        email.setText(PREMESSAGE + message + AFTERMESSAGE);

// embed images and include downloadable attachments
//        email.addEmbeddedImage("wink1", imageByteArray, "image/png");
//        email.addEmbeddedImage("wink2", imageDatesource);
//        email.addAttachment("invitation", pdfByteArray, "application/pdf");
//        email.addAttachment("dresscode", odfDatasource);

        try{
            new Mailer(SMTPURL, PORT, USERNAME, PWD).sendMail(email);

        } catch (Exception e){

        }
    }


}
